#include <time.h>
#include "CRebel.h"

#define IMAGE		"U1.png"
#define REBEL_MOVE			0
#define REBEL_SURPRISE		1
#define REBEL_TALK			2
#define REBEL_SITDOWN		3
#define REBEL_CRAWL			4
#define REBEL_FLY			5
#define REBEL_THROW			6
#define REBEL_THROW1		7
#define REBEL_TALKFONE		8
#define REBEL_STAND			9
#define REBEL_JUMP			10
#define REBEL_FEAR			11
#define REBEL_DIE			12
#define REBEL_DIE1			13
#define REBEL_DIE2			14
#define REBEL_DIE3			15

#define REBEL_STATE		20
#define LEFT			0
#define GROUND			600-265

CRebel::CRebel()
{
	m_FrameWidth = 50.0f;
	m_FrameHeight = 50.0f;
	m_iRebIndex = 0;
	m_fX = 0.0f;
	m_fY = 0;
	m_iX = 250;
	m_iY = GROUND;
	lastUpdate = 0;
	m_checkDie = false;
	timeBetweenUpdated = 220;
	m_Sprite = new CSprite[REBEL_STATE];
	SetRect();
}
CRebel::CRebel(LPD3DXSPRITE SurpriteHandler, float Scaling, CQuadTree* q)
{
	m_Quadtree = q;
	m_FrameWidth = 50.0f;
	m_FrameHeight = 50.0f;
	m_iRebIndex = 0;
	m_fScaling = Scaling;
	m_fX = 0.0f;
	m_fY = 0;
	m_iX = 470;
	m_iY = GROUND;
	lastUpdate = 0;
	timeBetweenUpdated = 220;
	m_Sprite = new CSprite[REBEL_STATE];
	LoadSprite(SurpriteHandler);
	SetRect();
	Talk();
}

CRebel::~CRebel()
{
	m_Sprite = NULL;
}

void CRebel::Reset()
{
	m_iX = 0;
	m_iY = 0;
}

void CRebel::LoadSprite(LPD3DXSPRITE ImageHandler)
{
	m_Sprite[REBEL_MOVE].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 13, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_SURPRISE].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 18, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_TALK].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 23, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_SITDOWN].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_CRAWL].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_FLY].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_THROW].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_THROW1].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_TALKFONE].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 30, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_STAND].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_JUMP].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 29, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_FEAR].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 30, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_DIE].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 30, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_DIE1].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 31, 30, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_DIE2].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 20, 0, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
	m_Sprite[REBEL_DIE3].CreateSprite(ImageHandler, IMAGE, m_FrameWidth, m_FrameHeight, 30, 0, m_fScaling, D3DCOLOR_XRGB(255, 255, 255));
}
void CRebel::Update()
{
	vector<Object*> result;
	m_Quadtree->CheckCollision(m_Quadtree->m_qRoot, *m_ObRect, 1, result);
	for(int i = 0; i < result.size(); i++)
	{
		Object* obj = result[i];
		obj->Collision(this);
	}
	if(m_checkDie ==true)
		Die();
	m_iX -= (int) m_fX;
	m_iY += (int) m_fY;
	SetRect();

	DWORD TimeNow = GetTickCount();

	if (TimeNow - lastUpdate > timeBetweenUpdated)
	{
		Next();
		lastUpdate = TimeNow;
	}
}
void CRebel::Render()
{
	m_Sprite[m_iRebIndex].Render(m_iX, m_iY);
}

void CRebel::Collision(Object* Ob)
{
	m_checkDie = true;
}
void CRebel::Next()
{
	m_Sprite[m_iRebIndex].Next();
	//line 0
	if(m_Sprite[m_iRebIndex].GetIndex() == 12 && m_iRebIndex == REBEL_MOVE)	//set index 
		m_Sprite[m_iRebIndex].SetIndex(0);
	//set index REBEL_SURPRISE
	if(m_Sprite[m_iRebIndex].GetIndex() == 17 && m_iRebIndex == REBEL_SURPRISE)
		m_Sprite[m_iRebIndex].SetIndex(13);

	if(m_Sprite[m_iRebIndex].GetIndex() == 22 && m_iRebIndex == REBEL_TALK)
		m_Sprite[m_iRebIndex].SetIndex(18);

	if(m_Sprite[m_iRebIndex].GetIndex() == 29 && m_iRebIndex == REBEL_SITDOWN)
		m_Sprite[m_iRebIndex].SetIndex(23);
	//line 1
	if(m_Sprite[m_iRebIndex].GetIndex() == 4 && m_iRebIndex == REBEL_CRAWL)
		m_Sprite[m_iRebIndex].SetIndex(0);

	if(m_Sprite[m_iRebIndex].GetIndex() == 14 && m_iRebIndex == REBEL_FLY)
		m_Sprite[m_iRebIndex].SetIndex(5);

	if(m_Sprite[m_iRebIndex].GetIndex() == 27 && m_iRebIndex == REBEL_THROW)
		m_Sprite[m_iRebIndex].SetIndex(15);
	//line 2
	if(m_Sprite[m_iRebIndex].GetIndex() == 17 && m_iRebIndex == REBEL_THROW1)
		m_Sprite[m_iRebIndex].SetIndex(0);

	if(m_Sprite[m_iRebIndex].GetIndex() == 29 && m_iRebIndex == REBEL_TALKFONE)
		m_Sprite[m_iRebIndex].SetIndex(18);
	//line 3
	if(m_Sprite[m_iRebIndex].GetIndex() == 6 && m_iRebIndex == REBEL_STAND)
		m_Sprite[m_iRebIndex].SetIndex(0);

	if(m_Sprite[m_iRebIndex].GetIndex() == 24 && m_iRebIndex == REBEL_JUMP)
		m_Sprite[m_iRebIndex].SetIndex(7);

	if(m_Sprite[m_iRebIndex].GetIndex() == 29 && m_iRebIndex == REBEL_FEAR)
		m_Sprite[m_iRebIndex].SetIndex(25);
	//
	if(m_Sprite[m_iRebIndex].GetIndex() == 15 && m_iRebIndex == REBEL_DIE)
		m_Sprite[m_iRebIndex].SetIndex(0);

	if(m_Sprite[m_iRebIndex].GetIndex() == 30 && m_iRebIndex == REBEL_DIE1)
		m_Sprite[m_iRebIndex].SetIndex(16);

	if(m_Sprite[m_iRebIndex].GetIndex() == 12 && m_iRebIndex == REBEL_DIE2)
		m_Sprite[m_iRebIndex].SetIndex(0);

	if(m_Sprite[m_iRebIndex].GetIndex() == 29 && m_iRebIndex == REBEL_DIE3)
		m_Sprite[m_iRebIndex].SetIndex(13);
}

void CRebel::SetRect()
{
	//vector<CRebel*> hg;
	m_ObRect = new RECT();
	m_ObRect->left = m_iX;
	m_ObRect->right = m_ObRect->left + m_FrameWidth;
	m_ObRect->top = m_iY;
	m_ObRect->bottom = m_ObRect->top + m_FrameHeight;
}
//line 0
void CRebel::Move()
{
	m_iRebIndex = REBEL_MOVE;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(0);
}

void CRebel::Suprise()
{
	m_iRebIndex = REBEL_SURPRISE;
	m_Sprite[m_iRebIndex].SetIndex(13);
	m_Sprite[m_iRebIndex].SetSpriteLine(0);
}

void CRebel::Talk()
{
	m_iRebIndex = REBEL_TALK;
	m_Sprite[m_iRebIndex].SetIndex(18);
	m_Sprite[m_iRebIndex].SetSpriteLine(0);
}
void CRebel::sitDown()
{
	m_iRebIndex = REBEL_SITDOWN;
	m_Sprite[m_iRebIndex].SetIndex(23);
	m_Sprite[m_iRebIndex].SetSpriteLine(0);
}
//line 1
void CRebel::Crawl()
{
	m_iRebIndex = REBEL_CRAWL;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(1);
}

void CRebel::Fly()
{
	m_iRebIndex = REBEL_FLY;
	m_Sprite[m_iRebIndex].SetIndex(5);
	m_Sprite[m_iRebIndex].SetSpriteLine(1);
}

void CRebel::Throw()
{
	m_iRebIndex = REBEL_THROW;
	m_Sprite[m_iRebIndex].SetIndex(15);
	m_Sprite[m_iRebIndex].SetSpriteLine(1);
}
//line 2
void CRebel::Throw1()
{
	m_iRebIndex = REBEL_THROW1;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(2);
}
 
void CRebel::TalkFone()
{
	m_iRebIndex = REBEL_TALKFONE;
	m_Sprite[m_iRebIndex].SetIndex(18);
	m_Sprite[m_iRebIndex].SetSpriteLine(2);
}

void CRebel::Stand()
{
	m_iRebIndex = REBEL_STAND;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(3);
}

void CRebel::Jump()
{
	m_iRebIndex = REBEL_JUMP;
	m_Sprite[m_iRebIndex].SetIndex(7);
	m_Sprite[m_iRebIndex].SetSpriteLine(3);
}
void CRebel::Fear()
{
	m_iRebIndex = REBEL_FEAR;
	m_Sprite[m_iRebIndex].SetIndex(25);
	m_Sprite[m_iRebIndex].SetSpriteLine(3);
}

//
void CRebel::Die()
{
	m_iRebIndex = REBEL_DIE;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(7);
}

void CRebel::Die1()
{
	m_iRebIndex = REBEL_DIE1;
	m_Sprite[m_iRebIndex].SetIndex(16);
	m_Sprite[m_iRebIndex].SetSpriteLine(7);
}

void CRebel::Die2()
{
	m_iRebIndex = REBEL_DIE2;
	m_Sprite[m_iRebIndex].SetIndex(0);
	m_Sprite[m_iRebIndex].SetSpriteLine(8);
}

void CRebel::Die3()
{
	m_iRebIndex = REBEL_DIE3;
	m_Sprite[m_iRebIndex].SetIndex(13);
	m_Sprite[m_iRebIndex].SetSpriteLine(8);
}